package com.wrial.test;
/*
 * @Author  Wrial
 * @Date Created in 9:42 2020/8/11
 * @Description
 */

public class QuickSort {

    public void myQuickSort(int arr[], int left, int right) {
        int pivot = arr[(left + right)/2];
        int pl = left;
        int pr = right;
        while (pl < pr) {
            while (arr[pl] < pivot) {
                pl++;
            }
            while (arr[pr] > pivot) {
                pr--;
            }
            if (pl > pr) break;
            int temp = arr[pl];
            arr[pl] = arr[pr];
            arr[pr] = temp;
            if (arr[pl] == pivot) {
                pl++;
            }
            if (arr[pr] == pivot) {
                pr--;
            }
        }
        if (pr > left) {
            myQuickSort(arr, left, pr);
        }
        if (pl < right) {
            myQuickSort(arr, pl, right);
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{-1, 8, 5, 6, 0, -9};
        new QuickSort().myQuickSort(arr, 0, arr.length - 1);
        for (int i : arr) {
            System.out.println(i);
        }
    }

}
